package com.atguigu.gmall.common.util;

//import com.baomidou.mybatisplus.core.toolkit.StringUtils;

import com.atguigu.gmall.common.constant.RedisConstant;
import com.atguigu.gmall.model.bo.UserIdBo;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;

/**
 * 获取登录用户信息类
 *
 */
public class AuthContextHolder {

    private static final ThreadLocal<UserIdBo> threadLocal = new ThreadLocal<>();

    public static void setUserIdBo(UserIdBo userIdBo){
        threadLocal.set(userIdBo);
    }

    public static UserIdBo getUserIdBo(){
       return threadLocal.get();
    }
    public static void removeUserIdBo(){
        threadLocal.remove();
    }


    /**
     * 获取当前登录用户id
     * @param request
     * @return
     */
    public static String getUserId(HttpServletRequest request) {
        String userId = request.getHeader(RedisConstant.USER_ID_REQUEST_HEADER);
        return StringUtils.isEmpty(userId) ? "" : userId;
    }

    /**
     * 获取当前未登录临时用户id
     * @param request
     * @return
     */
    public static String getUserTempId(HttpServletRequest request) {
        String userTempId = request.getHeader(RedisConstant.TEMP_USER_ID_HEADER);
        return StringUtils.isEmpty(userTempId) ? "" : userTempId;
    }
}
